<?php
require_once(PATH_typo3.'contrib/json/json.php');

class user_discountProducts_TCEforms {
	
	function user_getDiscountProductsField($PA, $pObj) {
		
			//Подключаем необходимые JS и CSS файлы
		if (!isset($pObj->additionalCode_pre['scriptaculous']))
		{
			$pObj->additionalCode_pre['scriptaculous'] = '
				<script type="text/javascript" src="'.t3lib_extMgm::extRelPath('user_farm_extjs').'scriptaculous.js?load=effects"></script>
			';
		}
		
		if (!isset($pObj->additionalCode_pre['prototypeAdapter']))
		{
			$pObj->additionalCode_pre['prototypeAdapter'] = '
				<script type="text/javascript" src="'.t3lib_extMgm::extRelPath('user_farm_extjs').'ext-prototype-adapter.js"></script>
			';
		}
		
		if (!isset($pObj->additionalCode_pre['extjs']))
		{
			$pObj->additionalCode_pre['extjs'] = '
				<script type="text/javascript" src="'.t3lib_extMgm::extRelPath('user_farm_extjs').'ext-all-debug.js"></script>
				<script type="text/javascript" src="'.t3lib_extMgm::extRelPath('user_farm_extjs').'locale/ext-lang-ru.js"></script>
			';
		}
		
		if (!isset($pObj->additionalCode_pre['extjscss']))
		{
			$pObj->additionalCode_pre['extjscss'] = '
				<link rel="stylesheet" type="text/css" href="'.t3lib_extMgm::extRelPath('user_farm_extjs').'resources/css/ext-all.css" />
				<link rel="stylesheet" type="text/css" href="'.t3lib_extMgm::extRelPath('user_farm_extjs').'resources/css/xtheme-gray.css" />
				<script type="text/javascript">
					Ext.BLANK_IMAGE_URL = "'.t3lib_extMgm::extRelPath('user_farm_extjs').'resources/images/gray/s.gif";
				</script>
			';
		}
		
		if(!isset($pObj->additionalCode_pre['discountProductsPanel'])){
			$pObj->additionalCode_pre['discountProductsPanel'] = '
				<script type="text/javascript" src="'.t3lib_extMgm::extRelPath('user_farm_commerce').'resources/scripts/discount-products-control.js">
				</script>
				<link rel="stylesheet" rel="text/css" href="'.t3lib_extMgm::extRelPath('user_farm_commerce').'resources/css/discount-products-cotrol.css" />
			';
		}
		
			//Инициализируем значение поля
		$fieldValue = '';
		$controlData = '[]';
		if (substr($PA['row']['uid'], 0, 3) != 'NEW') {
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
				'tx_commerce_products.uid AS id,
				 tx_commerce_products.user_farm_commerce_article AS article,
				 tx_commerce_products.title AS title',
				'tx_commerce_products',
				'user_farm_commerce_products_discounts_mm',
				'',
				' AND user_farm_commerce_products_discounts_mm.uid_foreign = "'.$PA['row']['uid'].'" '.t3lib_BEfunc::deleteClause('tx_commerce_products')
			);
			
			if (!$GLOBALS['TYPO3_DB']->sql_error()) {
				$tempValArray = array();
				$tempDataArray = array();
				
				while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
					$tempValArray[] = $row['id'];
					$tempDataArray[] = $row;
				}
				
				$fieldValue = implode(',', $tempValArray);
				
				$JSON = new Services_JSON();
				$controlData = $JSON->encode($tempDataArray);
			}
		}
			//Говорим TCEForms что поле обязательно для заполнения, в случае если скидка имеет тип "Скидка на товары"
		if ($PA['row']['discount_type'] == 2) {
			$pObj->requiredFields[$table.'_'.$row['uid'].'_'.$field] = $PA['itemFormElName'];	
		}
		
		return '
			<input type="hidden" name="data[user_farm_commerce_discounts]['.$PA['row']['uid'].'][products]" id="data[user_farm_commerce_discounts]['.$PA['row']['uid'].'][products]" value="'.$fieldValue.'" />
			<div id="data[user_farm_commerce_discounts]['.$PA['row']['uid'].'][products_control]"></div>
			<script type="text/javascript">
				var DiscountProductsField_'.$PA['row']['uid'].' = new Farm.DiscountProductsPanel({
					uid: "'.$PA['row']['uid'].'",
					selectedProducts: '.$controlData.' 
				});
			</script>
		';
	}
}
?>